//
//  MBCollectionView.m
//  添加图片
//
//  Created by MB__Lee on 2018/5/4.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBCollectionView.h"

#import "MBColleCell.h"
#import "MBPickCell.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height  [UIScreen mainScreen].bounds.size.height

#define length(a) Width/375.0 *a

@interface MBCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray *imageMUT;//用来存贮选择的照片
@property(nonatomic,strong)UIImagePickerController *picker;

@property(nonatomic,assign)CGFloat height;//用来记录存储当前的高度


@end

static NSString *MBColleCell_identi = @"MBColleCell";
static NSString *MBPickCell_identi = @"MBPickCell";

@implementation MBCollectionView
-(NSMutableArray *)imageMUT{
    if (!_imageMUT) {
        _imageMUT  = [NSMutableArray array];
        UIImage *image = [UIImage imageNamed:@"hetong_tianjia_icon"];
        [_imageMUT addObject:image];
    }
    return  _imageMUT;
}
-(UIImagePickerController *)picker{
    if (!_picker) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        _picker = picker;
        
    }
    return  _picker;
}

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{

    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    
    if (self) {
        //注册cell
        [self registerNib:[UINib nibWithNibName:MBColleCell_identi bundle:nil] forCellWithReuseIdentifier:MBColleCell_identi];
        [self registerNib:[UINib nibWithNibName:MBPickCell_identi bundle:nil] forCellWithReuseIdentifier:MBPickCell_identi];
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.delegate = self;
        self.dataSource = self;
    }
   
    
 
    return  self;
}

#pragma mark --代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return  1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageMUT.count > 6 ? 6 : self.imageMUT.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MBColleCell *cell = nil;
    
    NSInteger count = self.imageMUT.count - 2;//用来判断用哪种cell 的临界值
    if (indexPath.row > count) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:MBPickCell_identi forIndexPath:indexPath];
    }else{
        cell  = [collectionView dequeueReusableCellWithReuseIdentifier:MBColleCell_identi forIndexPath:indexPath];
    }
    
    
    cell.showIMG = self.imageMUT[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.deleteBlock = ^{
        [weakSelf.imageMUT removeObjectAtIndex:indexPath.row];
        [weakSelf collectionReloadAndLayout];
    };
    
    

    return  cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    
    if (self.MBEdgeInsets.right) {
        return  _MBEdgeInsets;
    }
    
    return  UIEdgeInsetsMake(5, 15, 15, 15);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row > self.imageMUT.count - 2 || (indexPath.row == 0 && self.imageMUT.count == 1)) {
        if ([self.MBDelegate respondsToSelector:@selector(MBCollectionView:presentPickerView:)]) {
//            [self.MBDelegate MBCollectionView:self presentPickerView:self.picker];
//            UIViewController *vc =  (UIViewController *)self.MBDelegate;
//            [vc presentViewController:self.picker animated:YES completion:nil];
            [self.MBDelegate MBCollectionView:self presentPickerView:self.picker];
            NSLog(@"picker");
        }
    }
    
    NSLog(@"coll");
}



#pragma mark --照片选择器
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *editImg = info[UIImagePickerControllerEditedImage];
    [self.imageMUT insertObject:editImg atIndex:self.imageMUT.count - 1];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    [self sendImgsToDelegate];//将图片传给外界
    [self collectionReloadAndLayout];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)sendImgsToDelegate{
    if ([self.MBDelegate respondsToSelector:@selector(MBCollectionView:sendImagesToDelegate:)]) {
        [self.MBDelegate MBCollectionView:self sendImagesToDelegate:self.imageMUT];
    }
}
//collectionView 刷新，然后重新布局frame
-(void)collectionReloadAndLayout{
    self.frame = [self caculateFrame];
    
    [self reloadData];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeMBCollectionHeight" object:@(self.height)];
    
    
}
//根据数组内容来计算collection 的frame
-(CGRect)caculateFrame{
    CGRect frame;//要返回的frame
    CGFloat height;// frame 的高度
    NSUInteger count = self.imageMUT.count;
    if (count < 4) {
        
        if (self.MBEdgeInsets.right) {
            height =  length(110) + self.MBEdgeInsets.top + self.MBEdgeInsets.bottom;
        }else{
            //110 item 大小，15 上边距，
            height = length(110) + length(15) + length(15);
        }
       
    }else {
        if (self.MBEdgeInsets.right) {
            height = 2 *length(110) + self.MBEdgeInsets.top + self.MBEdgeInsets.bottom  + length(7);
        }else{
             height = 2 * length(110) + length(15) + length(15) + length(7);
        }
        
       
    
    }
    frame = CGRectMake(0, 0, Width, height);
    
    self.height = height;
    return  frame;
}





@end
